package edu.sandau.basic.controller;

import edu.sandau.basic.domain.Role;
import edu.sandau.basic.domain.User;
import edu.sandau.basic.service.UserRoleService;
import edu.sandau.basic.vo.BasicMsg;
import edu.sandau.basic.vo.UserRoleVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api")
@Tag(name = "用户角色管理", description = "用户与角色关系的增删查改接口")
public class UserRoleController {

    @Autowired
    private UserRoleService userRoleService;

    @Operation(summary = "查询指定用户的角色")
    @GetMapping("/user_roles/{userId}")
    public List<Role> getUserRoles(@PathVariable Integer userId) {
        return userRoleService.getUserRoles(userId);
    }

    @Operation(summary = "更新用户角色（覆盖原有关系）")
    @PutMapping("/user_roles/{userId}")
    public BasicMsg updateUserRoles(@PathVariable Integer userId, @RequestBody UserRoleVo vo) {
        List<Integer> roleIds = vo.getRoleIds();
        userRoleService.updateUserRoles(userId, roleIds);
        return BasicMsg.ok("用户角色更新成功");
    }

    @Operation(summary = "删除用户所有角色绑定")
    @DeleteMapping("/user_roles/{userId}")
    public BasicMsg deleteUserRoles(@PathVariable Integer userId) {
        userRoleService.deleteUserRoles(userId);
        return BasicMsg.ok("已清空用户角色绑定");
    }

    @Operation(summary = "查询拥有指定角色的所有用户")
    @GetMapping("/role_users/{roleId}")
    public List<User> getUsersByRole(@PathVariable Integer roleId) {
        return userRoleService.getUsersByRole(roleId);
    }
}
